/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocator xmlns:vm="clr-namespace:BlogApp.WP8"
                           x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"

  You can also use Blend to do all this with the tool's support.
  See http://www.galasoft.ch/mvvm
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;

namespace BlogApp.WP8.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register(createMainViewModel);
            SimpleIoc.Default.Register(createCategoryViewModel);
            SimpleIoc.Default.Register(createPostViewModel);
            SimpleIoc.Default.Register(createSearchViewModel);
            SimpleIoc.Default.Register(createCommentViewModel);
        }

        private static CommentViewModel createCommentViewModel()
        {
            return  new CommentViewModel();
        }

        private static MainViewModel createMainViewModel()
        {
            return new MainViewModel();
        }

        private static CategoryViewModel createCategoryViewModel()
        {
            return new CategoryViewModel();
        }

        private static PostViewModel createPostViewModel()
        {
            return new PostViewModel();
        }

        private static SearchViewModel createSearchViewModel()
        {
            return new SearchViewModel();
        }

        /// <summary>
        /// Gets the CommentViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public static CommentViewModel CommentViewModel
        {
            get
            {

                return ServiceLocator.Current.GetInstance<CommentViewModel>();
            }
        }


        /// <summary>
        /// Gets the MainViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public static MainViewModel MainViewModel
        {
            get
            {

                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        /// <summary>
        /// Gets the CategoryViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public static CategoryViewModel CategoryViewModel
        {
            get
            {

                return ServiceLocator.Current.GetInstance<CategoryViewModel>();
            }
        }


        /// <summary>
        /// Gets the MainViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public static PostViewModel PostViewModel
        {
            get
            {

                return ServiceLocator.Current.GetInstance<PostViewModel>();
            }
        }
        
        /// <summary>
        /// Gets the SearchViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")]
        public static SearchViewModel SearchViewModel
        {
            get
            {
                return ServiceLocator.Current.GetInstance<SearchViewModel>();
            }
        }

        
        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }
}